<template>
  <div style="margin-top:30px;">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="名称" prop="title" style="margin-right:15px">
              <el-input v-model="form.title" placeholder="请输入名称" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="链接" prop="url" style="margin-right:15px">
              <el-input v-model="form.url" placeholder="请输入链接地址" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="图片" prop="img" style="margin-right:15px">
              <el-upload
                v-model="form.img"
                list-type="picture-card"
                :action="uploadUrl"
                :show-file-list="false"
                :on-success="handleImgSuccess"
                :before-upload="beforeImgUpload"
                :headers="getAuthorization()"
              >
                <img v-if="form.img" :src="form.img" class="el-upload-list__item-thumbnail" width="100%" height="100%">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="funcCancel">取 消</el-button>
      <el-button type="primary" @click="funcSubmit">确 定</el-button>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import request from '@/utils/request'
import { isURL } from '@/utils/validate4Form'
Vue.use(VXETable)
export default {
  name: 'PartnerShipsEdit',
  data() {
    return {
      uploadUrl: this.BASE_API_URL + 'ntocc-tps-api/fileUpload',
      emptyForm: {},
      form: {
        img: '',
        title: '',
        url: ''
      },
      rules: {
        img: [
          { required: true, message: '请上传图片', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入链接地址', trigger: 'blur' },
          { validator: isURL, trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 保存空表单对象
    this.emptyForm = JSON.parse(JSON.stringify(this.form))
  },
  methods: {
    // 获取页面信息
    getInfo(code) {
      // 查询到数据之前先展示空表单
      this.form = JSON.parse(JSON.stringify(this.emptyForm))
      request({ url: `/arpa-basic-api/partnerShips/${code}`, method: 'get' }).then(response => {
        this.form = response.data
      })
    },
    // 提交
    funcSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const data = this.form
          request({ url: '/arpa-basic-api/partnerShips', method: 'put', params: data }).then(response => {
            this.$message({ message: response.msg, type: 'success' })
            this.$emit('fatherEditMethod')
          })
        } else {
          return false
        }
      })
    },
    // 取消
    funcCancel() {
      this.$emit('fatherCancelMethod', 'edit')
    },
    handleImgSuccess(res, file) {
      this.$message({ message: res.msg, type: 'success' })
      this.form.img = res.data
    },
    // 上传验证
    beforeImgUpload(file) {
      const isJPG =
                    file.type === 'image/jpeg' ||
                    file.type === 'image/bmp' ||
                    file.type === 'image/png'
      const isLtnM = file.size / 1024 / 1024 < 10
      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG,BMP,PNG 格式!')
      }
      if (!isLtnM) {
        this.$message.error('上传图片大小不能超过 10MB!')
      }
      return isJPG && isLtnM
    }

  }
}
</script>
<style lang="scss" scoped>
  .dialog-footer {
    margin: 15px 0 15px 0;
    width: 175px;
    float: right;
  }
</style>
